﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SMSSnew.DATA.CLASSES;

namespace SMSSnew.UI.vicePrincipal.timetable.dataEntry
{
    public partial class vicePrinClassCombinationDetailsEdit : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["btnAdd"] != null)
            {
                control();
                ViewState.Add("falg", true);

            }
            
           
        
}
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
           

            timeTableCLZ objTimeTableClass = new timeTableCLZ();
            classCLZ objClassClass = new classCLZ();
            subjectCLZ objSubjClass = new subjectCLZ();

            DataTable dt;
            DataTable dt1;
            dt = objTimeTableClass.loadGrade();
            dt1 = objSubjClass.selectSubjectsWithTypes();

            if (dt.Rows.Count != 0)                         //load grades to combo box
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cboGrade.Items.Insert(i, dt.Rows[i][0].ToString());
                }
            }

            if (dt1.Rows.Count != 0)                        //load subjects to combo box
            {
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    cboSubject.Items.Insert(i, dt1.Rows[i][0].ToString());

                }
            }

          
            btnAdd.Visible = false;
              
           
            
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
           
                control();
           

            ViewState.Add("falg", true);

         }
        

        //public void runSearch()
        //{
        //    if (ViewState["falg"] == null)
        //    {
        //        control();
               
        //    }

        //    if (ViewState["falg"] != null)                      //to control dynamic properties
        //    {
                 
        //        control();
                

        //    }


        //    ViewState.Add("falg", true);
        //}

          
        public void control()               //creating dynamic properties
        {
            
            DataTable dt;
            DataTable dt1;
            DataTable dt2;


            classCLZ objClass = new classCLZ(cboGrade.SelectedItem.ToString());
            timeTableCLZ objTT = new timeTableCLZ();

            dt = objTT.loadClassID(objClass);
            dt2 = objClass.load_class_combination(cboGrade.SelectedValue.ToString(), cboSubject.SelectedValue.ToString());

            if (dt2.Rows.Count == 0)
            {
                Table table = new Table();
                table.ID = "tbl_fix";

                PH.Controls.Add(table);


                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                Label lbl = new Label();


                lbl.ID = "lbl_fix";

                lbl.Text = "There is no Class Combination in grade " + cboGrade.SelectedValue.ToString() + ", for " + cboSubject.SelectedValue.ToString();



                PH.Controls.Add(lbl);



                PH.Controls.Add(cell);
                PH.Controls.Add(row);

                btnAdd.Visible = false;
            }

            else
            {
               

                if (dt2.Rows.Count != 0)
                {
                    for (int i = 0; i < dt2.Rows.Count; i++)
                    {
                        Table table = new Table();
                        table.ID = "tbl_dy" + i;

                        PH.Controls.Add(table);

                        string clas = dt2.Rows[i][2].ToString();
                        TableRow row = new TableRow();
                        TableCell cell = new TableCell();
                        Label lbl = new Label();


                        lbl.ID = "lbl_" + i;

                        lbl.Text = clas;



                        PH.Controls.Add(lbl);



                        PH.Controls.Add(cell);
                        PH.Controls.Add(row);

                        PH.Controls.Add(new LiteralControl("&nbsp"));
                        PH.Controls.Add(new LiteralControl("&nbsp"));
                        PH.Controls.Add(new LiteralControl("&nbsp"));
                        TableCell cell1 = new TableCell();

                        DropDownList ddl = new DropDownList();

                        ddl.Width = 100;
                        ddl.Height = 25;
                        ddl.ID = "ddl_" + i;
                        ddl.Items.Add("select");
                        ddl.Items.Add("1");
                        ddl.Items.Add("2");
                        ddl.Items.Add("3");
                        ddl.Items.Add("4");
                        ddl.Items.Add("5");
                       

                        dt1 = objClass.select_class_combination(dt2.Rows[i][2].ToString(), dt2.Rows[i][0].ToString());

                        
                        ddl.ClearSelection();
                        string ww = ddl.SelectedValue.ToString();

                       
                        ddl.SelectedValue = dt1.Rows[0][0].ToString();
                       
                        string ww1 = ddl.SelectedValue.ToString();
                      

                        PH.Controls.Add(ddl);


                        PH.Controls.Add(cell1);
                       
                    }
                }
                btnAdd.Visible = true;





            }
 
        
        }


        
      


        DataTable edit_button_action()                  //action of edit button
        {
            
            

            DataTable dt1=new DataTable();
            DataTable dt2 = new DataTable();

            dt2.Columns.Add("1");
            dt2.Columns.Add("2");
            dt2.Columns.Add("3");

            classCLZ obj=new classCLZ();
         

            dt1= obj.load_class_combination(cboGrade.SelectedValue.ToString(),cboSubject.SelectedValue.ToString());

          for(int i=0; i<dt1.Rows.Count; i++)
          {
              DataRow dr = dt2.NewRow();
             

              DropDownList d = (DropDownList)PH1.FindControl("ddl_" + i);
              Label l = (Label)PH1.FindControl("lbl_"+i);

              string qq = d.SelectedValue.ToString();

              dr["1"] = cboSubject.SelectedValue.ToString();
              dr["2"] = d.SelectedValue.ToString();
              dr["3"] = l.Text.ToString();

              dt2.Rows.Add(dr);
              

          }
          return dt2;
      }
        


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            

            classCLZ obj = new classCLZ();
            obj.editClassCombination(edit_button_action());

            if (obj.editClassCombination(edit_button_action()) == "alert('Class is successfully edited')")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Class is successfully edited')", true);
                

            }
            if (obj.editClassCombination(edit_button_action()) == "alert('Class is not edited')")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Class is not edited')", true);
            }
            if (obj.editClassCombination(edit_button_action()) == "alert('Please select a group for all classes')")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('Please select a group for all classes')", true);
            }

        }


        protected void cboGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ViewState.Clear();
        }

        protected void cboSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ViewState.Clear();
        }

      

        
        public void dispose()
        {
            classCLZ objClass = new classCLZ();

            DataTable dt = new DataTable() ;
            dt = objClass.load_class_combination(cboGrade.SelectedValue.ToString(), cboSubject.SelectedValue.ToString());
            if (ViewState["falg"] != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList d = (DropDownList)PH.FindControl("ddl_" + i);
                    Label l = (Label)PH.FindControl("lbl_" + i);

                    d.Dispose();
                    l.Dispose();
                }
                ViewState.Clear();
            }
        }

    }

}
